package test;

import java.util.Random;

public class Demo {
    static Random random=new Random();

    public static void main(String[] args) {
        int arr[][]=new int[8][8];
        init(arr);
        for (int arr1[]:arr){
            for (int num:arr1){
                System.out.print(num+"\t");
            }
            System.out.println();
        }
        System.out.println("------------------");
        change(arr);
        for (int arr1[]:arr){
            for (int num:arr1){
                System.out.print(num+"\t");
            }
            System.out.println();
        }
    }

    //对二维数随机赋值
    public static void init(int arr[][]){
        for(int i=0;i<arr.length;i++){
            for (int j=0;j<arr[0].length;j++){
                int num=random.nextInt(1001);
                arr[i][j]=num;
            }
        }
    }

    //将最大值和最小值换位置
    public static void change(int arr[][]){
        int max=arr[0][0];
        int min=arr[0][0];
        int maxX=0;
        int maxY=0;
        int minX=0;
        int minY=0;
        for(int i=0;i<arr.length;i++){
            for (int j=0;j<arr[0].length;j++){
                if(arr[i][j]>max){
                    max=arr[i][j];
                    maxX=i;
                    maxY=j;
                }
                if(arr[i][j]<min){
                    min=arr[i][j];
                    minX=i;
                    minY=j;
                }
            }
        }
        arr[minX][minY]=max;
        arr[maxX][maxY]=min;
    }
}
